﻿using System;
using System.Collections.Generic;
using IntelligentStorWpfApp.Services;
using IntelligentStorWpfApp.Interface;
using IntelligentStorWpfApp.Model;
using System.Data;
using log4net;
using IntelligentStorWpfApp.Helper;

namespace IntelligentStorWpfApp.Controller
{
    public class ProductController
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(LinkProductPositionController));
        ProductInterface productInterface = new ProductInfoServices();

        public void UdateProduct(ProductInfo prod)
        {
            bool resut=productInterface.UpdateProduct(prod);
        }
        public bool addProductPosition(string productId,int PositionId)
        {
            try
            {
                bool result = productInterface.linkProductPosition(productId, PositionId);
                //if(result)
                //{
                //    if(!addProduct(product))
                //    {
                //        productInterface.deletelinkProductPosition(productId, PositionId);
                //        return false;
                //    }
                //    return true;
                //}
                //else
                //{
                //    return false;
                //}
                return result;
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
                return false;
            }
        }

        public bool GetProductByProductId(string pId)
        {

            return productInterface.GetProductByPId(pId);
        }

        public bool addProduct(ProductInfo product)
        {
            bool result = productInterface.AddProduct(product);
            if (result)
                LogHelper.AddLog(" 新增产品 " + product.ProductId + " success");
            else
                LogHelper.AddLog(" 新增产品 " + product.ProductId + " fail");

            return productInterface.AddProduct(product);
        }
        public List<ProductInfo> getAllProducts(ProductInfo product)
        {
            DataTable dt = productInterface.GetProducts(product);
            List<ProductInfo> listPrds= formatProductsToList(dt);
            return listPrds;
        }
        public List<ProductInfo> getProductsByUserName(string userName)
        {
            DataTable dt = productInterface.GetProductsByUser(userName);
            List<ProductInfo> listPrds = formatProductsToList(dt);
            return listPrds;
        }

        private List<ProductInfo> formatProductsToList(DataTable dt)
        {
            List<ProductInfo> listPrds = new List<ProductInfo>();
            try
            {
                ProductInfo productInfo;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    productInfo = new ProductInfo();
                    productInfo.ID = Convert.ToInt32(dt.Rows[i]["ID"]);
                    productInfo.Owner = dt.Rows[i]["UserName"].ToString();
                    productInfo.ProductId = dt.Rows[i]["ProductId"].ToString();
                    if (!string.IsNullOrEmpty(dt.Rows[i]["cabinetNum"].ToString()) && !string.IsNullOrEmpty(dt.Rows[i]["positionNum"].ToString()))
                    {
                        productInfo.Pposition = dt.Rows[i]["cabinetNum"].ToString() + "柜" + dt.Rows[i]["positionNum"].ToString() + "号";
                    }
                    productInfo.ProductNum = Convert.ToInt32(dt.Rows[i]["ProductNum"]);
                    productInfo.Status = dt.Rows[i]["Pstatus"].ToString();
                    productInfo.Description = dt.Rows[i]["Description"].ToString();
                    listPrds.Add(productInfo);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
            return listPrds;
        }

        public bool checkPosiontionId(int Id)
        {
            DataTable dt = productInterface.checkLink(Id);
            if(dt.Rows.Count>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public  List<ProductInfo> getAllonlyProducts()
        {
            DataTable dt = productInterface.getAllonlyProducts();
            return formatProducts(dt); 

        }

        public List<ProductInfo> getProductsByState(string state)
        {
            DataTable dt = productInterface.getProductsByStatus(state);
            return formatProducts(dt);
        }

        private List<ProductInfo> formatProducts(DataTable dt)
        {
            List<ProductInfo> listPrds = new List<ProductInfo>();
            ProductInfo productInfo;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                productInfo = new ProductInfo();
                productInfo.ProductId = dt.Rows[i]["ProductId"].ToString();
                productInfo.ProductNum = Convert.ToInt32(dt.Rows[i]["ProductNum"]);
                productInfo.Status = dt.Rows[i]["status"].ToString();
                listPrds.Add(productInfo);
            }
            return listPrds;
        }
        public List<string> getAllStatus()
        {
            List<string> lists = new List<string>();
            DataTable dt = productInterface.GetAllStatus();
            string status = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                status= dt.Rows[i]["status"].ToString();
                lists.Add(status);
            }
            return lists;
        }

    }
}
